package com.mark.springboot.service.impl;

import com.mark.springboot.entity.Admin;
import com.mark.springboot.mapper.AdminMapper;
import com.mark.springboot.request.LoginRequest;
import com.mark.springboot.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @Author: MARK
 * @Date: 2019/8/18 14:21
 * @Version: 1.0.0
 * @Description:
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Override
    public boolean login(LoginRequest loginRequest) {
        Example example = new Example(Admin.class);
        example.and().andEqualTo("username", loginRequest.getUsername());
        Admin admin1 = adminMapper.selectOneByExample(example);
        if(admin1 == null){
            return false;
        }else{
            if(getByUsername(loginRequest.getUsername())){
                return loginRequest.getPassword().equals(admin1.getPassword());
            }
            log.info("去哪了");
            return false;
        }
    }

    @Override
    public boolean add(Admin admin) {
        //用户名已存在 添加失败
        if(getByUsername(admin.getUsername())){
            return false;
        }
        admin.setCreateTime(LocalDate.now());
        admin.setUpdateTime(LocalDate.now());
        adminMapper.insert(admin);
        return true;
    }

    @Override
    public boolean resetPwd(Admin admin) {
        if(getByUsername(admin.getUsername())){
            admin.setUpdateTime(LocalDate.now());
            Example example = new Example(Admin.class);
            example.and().andEqualTo("username", admin.getUsername());
            adminMapper.updateByExampleSelective(admin, example);
            return true;
        }
        return false;
    }

    @Override
    public boolean update(Admin admin) {
        if(getByUsername(admin.getUsername())){
            Example example = new Example(Admin.class);
            admin.setUpdateTime(LocalDate.now());
            example.and().andEqualTo("username", admin.getUsername());
            adminMapper.updateByExampleSelective(admin, example);
            return true;
        }
        return false;
    }

    @Override
    public List<Admin> getList() {
        return adminMapper.selectAll();
    }

    @Override
    public boolean batchDelete(List<Integer> ids) {
        Example example = new Example(Admin.class);
        example.and().andIn("id", ids);
        adminMapper.deleteByExample(example);
        return true;
    }

    //判断是否有该用户名
    @Override
    public boolean getByUsername(String username) {
        Example example = new Example(Admin.class);
        example.and().andEqualTo("username", username);
        Admin admin = adminMapper.selectOneByExample(example);
        //返回true 已经有该用户名
        return admin != null;
    }
}
